package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.EditConfigPO;
import com.falcon.domain.dos.EditConfigDO;
import com.falcon.domain.req.EditConfigQuery;
import com.falcon.domain.req.EditConfigRequest;

import com.falcon.mapper.IEditConfigMapper;
import com.falcon.service.IEditConfigService;


@Slf4j
@Service
public class EditConfigService implements IEditConfigService {

    @Autowired
    private IEditConfigMapper editConfigMapper;

    @Override
    public Long save(EditConfigRequest request){
        EditConfigPO editConfigPO = BeanUtil.copyProperties(request, EditConfigPO.class);

        this.editConfigMapper.insertNotNull(editConfigPO);

        return editConfigPO.getId();
    }

    @Override
    public Boolean update(Long id, EditConfigRequest request){

        EditConfigPO editConfigPO = BeanUtil.copyProperties(request, EditConfigPO.class);
        editConfigPO.setId(id);

        return this.editConfigMapper.updateNotNull(editConfigPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.editConfigMapper.deleteById(id);
    }

    @Override
    public EditConfigDO queryById(Long id){

        EditConfigPO editConfigPO = this.editConfigMapper.selectById(id);

        EditConfigDO editConfigDO = BeanUtil.copyProperties(editConfigPO, EditConfigDO.class);

        return editConfigDO;
    }


    @Override
    public List<EditConfigDO> queryList(EditConfigQuery request){

        Condition condition = request.toCondition();

        List<EditConfigPO> poList = this.editConfigMapper.selectList(condition);

        List<EditConfigDO> doList = BeanUtil.copyToList(poList, EditConfigDO.class);

        return doList;
    }

    @Override
    public BasePager<EditConfigDO> queryPager(EditConfigQuery request){


        PageInfo<EditConfigPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.editConfigMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, EditConfigDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.editConfigMapper.deleteByIds(ids);
    }


}
